﻿Public Class FrmMeses

    Private result As GBDZafiro.Resultado

    Private Sub FrmMeses_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargarGv()
    End Sub

    Protected Overrides Sub bnNuevo_Click(sender As Object, e As System.EventArgs)
        Dim i As Integer = gvMeses.Rows.Count
        Select Case i
            Case 0
                gvMeses.Rows.Add("00", "INICIO", False, False)
            Case 1
                gvMeses.Rows.Add("01", "ENERO", False, False)
            Case 2
                gvMeses.Rows.Add("02", "FEBRERO", False, False)
            Case 3
                gvMeses.Rows.Add("03", "MARZO", False, False)
            Case 4
                gvMeses.Rows.Add("04", "ABRIL", False, False)
            Case 5
                gvMeses.Rows.Add("05", "MAYO", False, False)
            Case 6
                gvMeses.Rows.Add("06", "JUNIO", False, False)
            Case 7
                gvMeses.Rows.Add("07", "JULIO", False, False)
            Case 8
                gvMeses.Rows.Add("08", "AGOSTO", False, False)
            Case 9
                gvMeses.Rows.Add("09", "SEPTIEMBRE", False, False)
            Case 10
                gvMeses.Rows.Add("10", "OCTUBRE", False, False)
            Case 11
                gvMeses.Rows.Add("11", "NOVIEMBRE", False, False)
            Case 12
                gvMeses.Rows.Add("12", "DICIEMBRE", False, False)
            Case 13
                gvMeses.Rows.Add("13", "CIERRE", False, False)
        End Select


    End Sub

    Protected Overrides Sub bnGuardar_Click(sender As Object, e As System.EventArgs)
        If guardar() Then
            MyBase.bnGuardar_Click(sender, e)
            cargarGv()
        End If
    End Sub

    Private Sub btEditar_Click(sender As System.Object, e As System.EventArgs) Handles btEditar.Click
        gvMeses.CurrentRow.ReadOnly = False
    End Sub

    Private Sub btCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btCancelar.Click
        gvMeses.CurrentRow.ReadOnly = True
    End Sub

    Private Sub gvMeses_RowLeave(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gvMeses.RowLeave
        gvMeses.CurrentRow.ReadOnly = True
    End Sub

    Protected Overrides Sub bnEliminar_Click(sender As Object, e As System.EventArgs)
        If msnBox.msnQuestion("¿Desea borrar el mes " + gvMeses.CurrentRow.Cells("DESCRIPCION").Value + "?") = _
            Windows.Forms.DialogResult.Yes Then
            result = GBDZafiro.Meses.Eliminar(crearMes())

            If result.ExisteError Then
                msnBox.msnError(result.Mensaje)
            Else
                MyBase.bnEliminar_Click(sender, e)
                cargarGv()
            End If
        End If
    End Sub

    Private Sub cargarGv()
        Dim est, may As Boolean
        gvMeses.Rows.Clear()
        For Each r As DataRow In GBDZafiro.Meses.ConsultarAll().Rows
            If r("ESTADO") = "A" Then
                est = True
            Else
                est = False
            End If

            If r("MAYORIZA") = "S" Then
                may = True
            Else
                may = False
            End If

            gvMeses.Rows.Add(r("COD_MES"), r("DESCRIPCION"), est, may)
        Next

        For Each r As DataGridViewRow In gvMeses.Rows
            r.ReadOnly = True
        Next

    End Sub

    Private Function guardar() As Boolean
        Dim mes As GBDZafiro.Meses
        mes = crearMes()
        If GBDZafiro.Meses.Exist(mes) Then
            result = GBDZafiro.Meses.Actualizar(mes)
        Else
            result = GBDZafiro.Meses.Nuevo(mes)
        End If

        If result.ExisteError Then
            msnBox.msnError(result.Mensaje)
        End If

        Return Not result.ExisteError
    End Function

    Private Function crearMes() As GBDZafiro.Meses
        Dim row As DataGridViewRow = gvMeses.CurrentRow
        Dim mes As New GBDZafiro.Meses
        mes.COD_MES = row.Cells("COD_MES").Value
        mes.DESCRIPCION = row.Cells("DESCRIPCION").Value

        If row.Cells("ESTADO").Value Then
            mes.ESTADO = "A"
        Else
            mes.ESTADO = "C"
        End If

        If row.Cells("MAYORIZA").Value Then
            mes.MAYORIZA = "S"
        Else
            mes.MAYORIZA = "N"
        End If

        Return mes
    End Function
   
End Class
